■ 文字色、背景色、フォント、太字、イタリック、下線等 装飾
| ラベル、ボタン文字の装飾に関連した下記方法について紹介します。 ・文字色及び文字の背景色変更 ・フォント及びフォントサイズ変更 ・太字化、イタリック化、下線追加 ★ボタンの背景色も設定できます。 |
||
private void button1_Click(object sender, EventArgs e)
{
//ラベル
label1.Text = "Hellow World !!";
label1.ForeColor = Color.Red; //文字色→赤
label1.BackColor = Color.Yellow;//背景色→黄
label1.Font = new Font("MS ゴシック", 14, //フォント サイズ
FontStyle.Bold | FontStyle.Italic | FontStyle.Underline);
//太字、イタリック、下線
//ボタン
button1.ForeColor = Color.Blue;//文字色→青
button1.BackColor = Color.LightGreen;//背景色→薄緑
// label1.Font = new Font("MS ゴシック",18);
// label1.Font = new Font(label1.Font, FontStyle.Italic);
// label1.Font = new Font(label1.Font, FontStyle.Bold);
// label1.Font = new Font(label1.Font, FontStyle.Strikeout); //取消線
// 標準の太さ&非イタリックへ戻る
// label1.Font = new Font(label1.Font, FontStyle.Regular);
}
|
![]() |
| 文字や配列を文字列に変換する ★ char Chr = 'A'; とした時 Chrはクラスのインスタンスとして定義されます。C言語の文字型のcharではありません。 |
||
private void button1_Click(object sender, EventArgs e)
{
char charA = 'A';
char charB = 'B';
char charC = 'C';
textBox1.Text = charA.ToString() //文字型→文字列
+ charB.ToString()
+ charC.ToString();
//----------------------------------------------------
char[] charD = {'D'};
char[] charE = {'E'};
char[] charF = {'F'};
string strD = new string(charD); //配列→文字列
string strE = new string(charE);
string strF = new string(charF);
textBox2.Text = strD + strE + strF;
//-----------------------------------------------------------------
char [] myArray = { 'G', 'H', 'I' };
string myString = new string(myArray); //配列→文字列
textBox3.Text = myString;
}
|
![]() |
| <実行結果> | |
<プログラム例>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int I1 = 65; // 0x41; //A
int I2 = 66; // 0x42; //B
int I3 = 67; // 0x43; //C
char code1 = (char)I1; //整数を文字(インスタンス)にキャストする
char code2 = (char)I2;
char code3 = (char)I3;
textBox1.Text = I1.ToString(); //整数を文字列に変換する
textBox2.Text = I2.ToString();
textBox3.Text = I3.ToString();
textBox4.Text = code1.ToString(); //文字(インスタンス)を文字列に変換する
textBox5.Text = code2.ToString();
textBox6.Text = code3.ToString();
textBox7.Text = Convert.ToString(I1,16); //整数を16進表示の文字列に変換する
textBox8.Text = Convert.ToString(I2,16);
textBox9.Text = Convert.ToString(I3,16);
}
}
}
|
![]() |
■ 文字列の書式
| public static string. Format( string format, Object arg0 ) ★ 第1引数内の記述方法 ・n番目の第2引数を呼ぶとき {n}とする。 ・{n}は順不同で何回でも呼び出せる ・{n:XXXX}のように表示フォーマットを規定する。// XXX記載なし → G(genereral 一般) ・XXXXには G3 と#.##0 のような2種類の表示方式がある。前者は「標準数値書式指定文字列」、 後者は「カスタム数値書式指定文字列」と呼ばれる。 ・\n、\r、\t などのエスケープ文字が使用できる ★ string.Format( )の書式は.NETのVB、VC++にも同様のものが定義されています。 |
|
private void button1_Click(object sender, EventArgs e)
{
int Year = 2008;
int Month = 10;
int Day = 5;
string str
= String.Format
("今日は{0}年{1}月{2}日です。\n明日は{0}年{1}月{3}日です。"
, Year,Month,Day,Day+1);
label1.Text = str;
}
|
![]() |
private void button1_Click(object sender, EventArgs e)
{
//------整数の表示
int iVal1 = -12345;
string str1 = string.Format("iVal1 = {0:d} です",iVal1);
label1.Text = str1;
//------少数点以下の桁表示
double fVal = -1234.5678;
string str2 = string.Format("fVal = {0:f3} です" ,fVal); //小数点以下3桁
label2.Text = str2;
//------16進数の表示(小文字表示)
int iVal3 = 255;
string str3 = string.Format("iVal3 = {0:x} です", iVal3);
label3.Text = str3;
//------16進数の表示(大文字表示)
int iVal4 = 254;
string str4= string.Format("iVal4 = {0:X} です", iVal4);
label4.Text = str4;
}
|
![]() |
private void button1_Click(object sender, EventArgs e)
{
//-------3桁区切り,の挿入
double iVal1 = 123456789;
string str1 = string.Format("iVal1 = {0:#,0}(3桁区切り)", iVal1); //デフォルト=3(桁)
label1.Text = str1;
// もともとのカルチャをバックアップ
System.Globalization.CultureInfo backup = System.Globalization.CultureInfo.CurrentCulture;
// 現在のスレッドのカルチャを書き換える
System.Globalization.CultureInfo ci =
(System.Globalization.CultureInfo)System.Globalization.CultureInfo.CurrentCulture.Clone();
ci.NumberFormat.NumberGroupSizes = new int[] { 4 }; //3桁区切り →4桁区切り
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
//-------4桁区切り,の挿入
double iVal2 = 123456789;
string str2 = string.Format("iVal2 = {0:#,0}(4桁区切り)", iVal2);
label2.Text = str2;
// もともとのカルチャを復元 //4桁区切り →3桁区切り
System.Threading.Thread.CurrentThread.CurrentCulture = backup;
//-------3桁区切り,の挿入
double iVal3 = 123456789;
string str3 = string.Format("iVal3 = {0:#,0}(3桁区切り)", iVal3);
label3.Text = str3;
}
|
![]() |
<追記>
(1) String.Formatの構文
・ 基本構文 : String.Formata("「書式指定文字列」",引数1,引数2,……))
・ 書式指定項目の構文 : {インデックス番号, 書式指定子:精度指定子}
・ 書式指定には「標準数値書式指定文字列」と「カスタム数値書式指定文字列」の2種類があります。
| 書式指定子 | 名前 | 説明 |
| Dまたはd | 10進数 (整数) | この書式指定は整数型でだけサポートされています。数値は、0 〜 9 の数字から成る文字列に変換されます。負の数値の場合は、文字列の先頭にマイナス記号が挿入されます。精度指定子は、変換後の文字列の最小桁数を示します。必要に応じて、精度指定子によって指定された桁数に達するまで、数値の左側にゼロが埋め込まれます。 |
| E または e | 指数 | 数値は、"-d.ddd…E+ddd" または "-d.ddd…e+ddd" という形式の文字列に変換されます。精度指定子は、小数部の桁数を示します。書式指定子が大文字の場合は指数部の前に 'E' が挿入され、小文字の場合は 'e' が挿入されます。 |
| F または f | 固定小数点 (浮動少数) | 数値は、"-ddd.ddd..." という形式の文字列に変換されます。この 'd' は 0 〜 9 までの数字を示します。負の数値の場合、変換後の文字列の先頭にマイナス記号が挿入されます。精度指定子は、小数部の桁数を示します。 |
| gまたはG | 一般 | 数値は、数値の型や、精度指定子が指定されているかどうかに応じて、固定小数点表記または指数表記のいずれかの最も簡潔な形式に変換されます。指数表記が使用される場合、結果の指数部には、書式指定子が 'G' のときには 'E'、書式指定子が 'g' のときには 'e' というプリフィックスが付きます。 |
| N または n | 数値 | 数値は、"-d,ddd,ddd.ddd…" という形式の文字列に変換されます。"-" は負数記号を示し (必要な場合)、"d" は数字 (0 〜 9) を示し、"," は数値グループ間の桁区切り記号を示し、"." は小数点記号を示します。精度指定子は、小数部の桁数を示します。 |
| P または p | パーセント | 数値は、パーセント値を表す文字列に変換されます。パーセント値を示すため、変換後の数値に 100 が乗算されます。 |
R または r | ラウンド トリップ | ラウンドトリップ指定子は、数値の変換後の文字列が、変換前の数値へ戻るように解析されることを指定します。精度指定子は、指定できますが無視されます。 |
X または x | 16 進数 | この書式指定は整数型でだけサポートされています。数値は 16 進数文字列に変換されます。精度指定子は、変換後の文字列の最小桁数を示します。必要に応じて、精度指定子によって指定された桁数に達するまで、数値の左側にゼロが埋め込まれます。 |
以下に書式指定項目の記述例を紹介します。
num: 整数 num = 13; |
|
■文字列を数値(int,long ,float,double)に変換する
| 文字列を数値化する方法には 以下の方法があります。 ?Parseメソッドを使う ?Convertクラスを使う ★ doubleとint,long,float,など他の型との混合演算は キャスト化も含めしないこと 正しい演算結果が得られないことがあるようです。 下段のrichTextBox2のような結果となってしまいました。 |
|
|
<プログラム例>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Numerical_to_String
{
public partial class Form1 : Form
{
//int型に変換
int I_1 = int.Parse("1");
// long型に変換
long L_20 = long.Parse("20");
// float型に変換
float F_300_3 = float.Parse("300.3");
// double型に変換
double D_1 = int.Parse("1");
double D_20 = double.Parse("20");
double D_300_3 = double.Parse("300.3");
double D_4000_04 = double.Parse("4000.04");
//// int型に変換
//int I_1 = Convert.ToInt32("1");
// // long型に変換
//long L_20 = Convert.ToInt64("20");
// // float型に変換
//float F_300_3 = Convert.ToSingle("300.3");
// // double型に変換
//double D_4000_04 = Convert.ToDouble("4000.04");
public Form1()
{
InitializeComponent();
label1.Text = ("doubleと int,long,floatが混在する演算は\n誤差が発生することがある。");
}
private void button1_Click(object sender, EventArgs e)
{
// float Total = I_1 + L_20 + F_300_3 + (float)D_4000_04; //doubleは、floatに変換して演算のこと
double Total = D_1 + D_20 + D_300_3 + D_4000_04; //doubleは すべてdoubleで演算のこと
textBox1.Text = Total.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
//doubleとint,long,float,など他の型との演算は キャスト化も含めしないこと
//誤差が発生し、期待する演算結果が得られない ---> 4321.34 vs 4321.33998779297
double Total2 = (double)I_1 + (double)L_20 + (double)F_300_3 + D_4000_04;
textBox2.Text = Total2.ToString();
}
}
}
|
|
| 実行結果 | |
![]() |
| Convertクラスを使う方法とParseメソッドを使う方法があります。 16進数以外の文字列を変換しようとすると例外が発生します。 |
|
|
<プログラム例>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//hexadecimal number to decimal number
namespace _16_WindowsFormsApp1
{
public partial class Form1 : Form
{
int num;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
num = Convert.ToInt32("F", 16); //16進数文字列を 10進数に変換
// num = int.Parse("F", System.Globalization.NumberStyles.HexNumber); //16進数文字列を 10進数に変換
textBox1.Text = num.ToString();
// num = Convert.ToInt32("FF", 16);//16進数文字列を 10進数に変換
num = int.Parse("FF", System.Globalization.NumberStyles.HexNumber);//16進数文字列を 10進数に変換
textBox2.Text = num.ToString();
// num = Convert.ToInt32("4D2", 16);//16進数文字列を 10進数に変換
num = int.Parse("4D2", System.Globalization.NumberStyles.HexNumber);//16進数文字列を 10進数に変換
textBox3.Text = num.ToString();
}
}
}
|
|
| 実行結果 | |
![]() |
| ConvertクラスのToStringメソッドやInt32構造体等のToStringメソッドで変換できます。 | |
|
<プログラム例>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int num = 127;
textBox1.Text = num.ToString("x"); //小文字で表示
//string str = Convert.ToString(num, 16); //Convertクラスを使っても可
//textBox1.Text = str;
textBox2.Text = num.ToString("X"); //大文字で表示
textBox3.Text = num.ToString("x4"); //小文字で表示 桁数4
textBox4.Text = num.ToString("X4"); //大文字で表示 桁数4
}
}
}
|
|
| 実行結果 | |
|
| ・ANSI文字を表示するにはKeyPressイベントをつかいe.Charで入力文字を取得します。KeyDownイベントやKeyUpイベントではe.Charから文字取得はできません。 | 実行結果 | |
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
label1.UseMnemonic = false; //trueだと(Shift+6)の時”&”が表示されません。
//デフォルトではtrueです。
string str = String.Format("{0} = {1}", "e.KeyChar", e.KeyChar);
label1.Text = str;
}
|
<shift + A 打鍵の場合>![]() <shift + 6 打鍵の場合> ![]() |
|
| ・ANSI文字に加え、ファンクションキー、方向キー、Insert、Home、Shift等を含めたすべての文字検出は、KeyDownイベントやKeyUpイベントからe.KeyCodeをつかって入力文字を取得します。 ・文字の判別には下表のKeys列挙体のメンバーを使います。 ・入力キーの名称はToString( )から得ることもできます。 ・修飾キーのShift Control AltについてはKeyEventArgsのプロパティをつかって以下のように判別することもできます。 if(e.shift == 1) //シフトキーが押されたなら if(e.Control == 1) //コントロールキーが押されたなら if(e.Alt == 1) //ALTが押されたなら |
||
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
string str1 = String.Format("{0}(16進数)= {1:X}", "e.KeyCode", e.KeyCode);
label1.Text = str1;
string str2 = String.Format("{0}(整数)= {1:D}", "e.KeyCode", e.KeyCode);
label2.Text = str2;
string str3 = String.Format("{0}(文字)= {1}", "e,KeyCode.ToString()", e.KeyCode.ToString());
label3.Text = str3;
}
|
< 打鍵キー = K >![]() < 打鍵キー = Delet > ![]() |
|
| 所定の文字かどうか判別する場合の例です。 フォームのKeyPreviewプロパティをtrueにして、フォームのイベントハンドラでキー入力を検出します。 |
||
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace KeyDetect
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.KeyPreview = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Enter:
listBox1.Items.Add("keys.Enter"); break; //Enterキー
case Keys.A:
listBox1.Items.Add("Keys.A"); break; //A
case Keys.D3:
listBox1.Items.Add("Keys.D3"); break; //3
case Keys.NumPad3:
listBox1.Items.Add("Keys.NumPad3"); break; //Numキーの3
case Keys.F12:
listBox1.Items.Add("Keys.F12"); break; //ファンクションキーF12
default: break;
}
}
}
}
|
![]() |
<Keys列挙体メンバー>
| キーボード表示 | Keys列挙体のメンバー | 備考 |
| A - Z | A - Z | |
| 0 1 2 3 4 5 6 7 8 9 | D0 D1 D2 …… D8 D9 | |
| F1 F2 F3 F10 F11 F12 | F1 F2 F3 F4 …… | |
| Shift Control Alt | ShiftKey ControlKey AltKey | |
| テンキー 0 1 2 3 4 5 6 7 8 9 |
NumPad0 NumPad1 …… NumPad9 | |
| BackSpace PrintScreen | Back PrintScreen |
他
■ 整数の読み込み
| result Int32.TryParse メソッド (String, Int32) // Stringが整数か否かを判別、 戻り値:正常(整数)ならtrue, その他(非整数)ならfalse | 実行結果 | |
|
<プログラム例>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace int_tryToParse
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
richTextBox1.Multiline = true;
}
private void TryToParse(string value)
{
int number;
bool result = Int32.TryParse(value, out number);
if (result)
{
richTextBox1.SelectionColor = Color.Black; //SelectionColorはリッチテキストのみ
richTextBox1.SelectedText += string.Format("Converted '{0}' to {1}.\r\n", value, number); //改行して順次書き込み
}
else
{
richTextBox1.SelectionColor = Color.Red; //
if (value == null) value = "";
richTextBox1.SelectedText += string.Format("Attempted conversion of '{0}' failed\r\n.", value); //改行して順次書き込み
}
}
private void button1_Click(object sender, EventArgs e)
{
TryToParse(null);
TryToParse("160519");
TryToParse("9432.0");
TryToParse("16,667");
TryToParse(" -322 ");
TryToParse("+4302");
TryToParse("(100);");
TryToParse("01FA");
}
}
}
|
![]() |
■ 浮動小数読込
| result Double.TryParse(String,Double); //// Stringが浮動小数か否かを判別、 戻り値:正常(整数)ならtrue, その他(非整数)ならfalse | 実行結果 | |
|
<プログラム例>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Double_tryParse
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
richTextBox1.Multiline = true;
}
private void TryToParse(string value)
{
double number;
bool result = Double.TryParse(value, out number);
if (result)
{
richTextBox1.SelectionColor = Color.Black; //SelectionColorはリッチテキストのみ
richTextBox1.SelectedText += string.Format("Converted '{0}' to {1}.\r\n", value, number); //改行して順次書き込み
}
else
{
richTextBox1.SelectionColor = Color.Red; //
if (value == null) value = "";
richTextBox1.SelectedText += string.Format("Attempted conversion of '{0}' failed\r\n.", value); //改行して順次書き込み
}
}
private void button1_Click_1(object sender, EventArgs e)
{
TryToParse(null);
TryToParse("160.519");
TryToParse("9432.0");
TryToParse("16,667");
TryToParse(" -32.254 ");
TryToParse("+4302");
TryToParse("(100);");
TryToParse("01FA");
}
}
}
|
![]() |
■ テキストボックスから浮動小数読込(有効数字小数点以下2桁)
| テキストボックスに入力した浮動小数を小数点3桁以降を四捨五入しててテキストボックスに有効数字2桁再表示するプログラムです。 |
実行結果 | |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _2digit_double__TEXT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.KeyPreview = true;
textBox1.TextAlign = HorizontalAlignment.Right; //右寄せ
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
double number;
bool result = Double.TryParse(textBox1.Text, out number);
if (result)
{
textBox1.Text = string.Format("{0:F2}", number); //小数点3桁以降四捨五入
}
else
{
MessageBox.Show("浮動小数を入力してください", "警告", MessageBoxButtons.OK);
}
}
}
}
}
|
![]() 小数点3桁以降四捨五入前 ![]() 小数点3桁以降四捨五入後 |
| 正規表現を使って、ソースコードから 16進数表示部(0xXX)をフィルタリングして摘出したプログラムです。 |
|
<プログラム例>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;//追加
namespace _0xXX摘出1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//空行は残した場合
Regex re = new Regex(@"\{(?<moji>[0-9A-Fa-fx\r\s,]+)\}"); //正規表現で0xXX をフィルタリング
string st = re.Match(richTextBox1.Text).Result("${moji}").TrimStart().TrimEnd();
richTextBox2.Text = st;
// MessageBox.Show(st);
////空行を残さない場合
////"{"から"}"までの間を丸ごと抜き出す//"0x[0-9a-fA-F][0-9a-fA-F],"または"\r\n"の1回以上の繰り返しがあって、"{"から始まって"}"で終わるまでの間
//System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex("0x[0-9a-fA-F][0-9a-fA-F],( |\r\n)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
//System.Text.RegularExpressions.Match m = r.Match(richTextBox1.Text);
//richTextBox1.Text = string.Empty;
//while (m.Success)
//{
// richTextBox1.Text += m.Value;
// m = m.NextMatch();
//}
}
private void button2_Click(object sender, EventArgs e)
{
// OpenFileDialog の新しいインスタンスを生成する (デザイナから追加している場合は必要ない)
OpenFileDialog openFileDialog1 = new OpenFileDialog();
// ダイアログのタイトルを設定する
openFileDialog1.Title = "ダイアログのタイトルをココに書く";
// 初期表示するディレクトリを設定する
openFileDialog1.InitialDirectory = @"C:\";
// 初期表示するファイル名を設定する
openFileDialog1.FileName = "初期表示するファイル名をココに書く";
// ファイルのフィルタを設定する
openFileDialog1.Filter = "テキスト ファイル|*.txt;*.log|すべてのファイル|*.*";
// ファイルの種類 の初期設定を 2 番目に設定する (初期値 1)
openFileDialog1.FilterIndex = 2;
// ダイアログボックスを閉じる前に現在のディレクトリを復元する (初期値 false)
openFileDialog1.RestoreDirectory = true;
// 複数のファイルを選択可能にする (初期値 false)
openFileDialog1.Multiselect = true;
// [ヘルプ] ボタンを表示する (初期値 false)
openFileDialog1.ShowHelp = true;
// [読み取り専用] チェックボックスを表示する (初期値 false)
openFileDialog1.ShowReadOnly = true;
// [読み取り専用] チェックボックスをオンにする (初期値 false)
openFileDialog1.ReadOnlyChecked = true;
// 存在しないファイルを指定した場合は警告を表示する (初期値 true)
//openFileDialog1.CheckFileExists = true;
// 存在しないパスを指定した場合は警告を表示する (初期値 true)
//openFileDialog1.CheckPathExists = true;
// 拡張子を指定しない場合は自動的に拡張子を付加する (初期値 true)
//openFileDialog1.AddExtension = true;
// 有効な Win32 ファイル名だけを受け入れるようにする (初期値 true)
//openFileDialog1.ValidateNames = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK) //ファイル選択のダイアログを表示
//戻り値がOKの場合は選択したファイルを表示する
{
// StreamReader の新しいインスタンスを生成する
System.IO.StreamReader cReader = (
new System.IO.StreamReader(openFileDialog1.FileName, System.Text.Encoding.Default)
);
// ファイルの最後まで読み込む
string stBuffer = cReader.ReadToEnd();
// cReader を閉じる (正しくは オブジェクトの破棄を保証する を参照)
cReader.Close();
richTextBox1.Text = stBuffer; //リッチテキストに読み込まれたファイルを表示
// ダイアログのタイトルにファイル名をフルパスで表示する
string s = openFileDialog1.FileName;
this.Text = s;
}
// 不要になった時点で破棄する (正しくは オブジェクトの破棄を保証する を参照)
openFileDialog1.Dispose();
}
private void button3_Click(object sender, EventArgs e) //改行コード・空白・水平タブコード削除ボタン
{
string str = richTextBox2.Text;
richTextBox2.Clear();
str = str.Replace("\n", ""); //改行コード削除
str = str.Replace(" ", ""); //空白削除
str = str.Replace("\t", ""); //水平タブキー削除
richTextBox2.Text = str;
}
}
}
|
|
<実行結果>![]() |
<プログラム例>
main() { }